package com.mike.billingserver.configutation;

import com.mike.billingserver.dto.RequestDTO;
import com.mike.billingserver.util.ContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;

import java.util.Optional;

/**
 * @author xiaozefeng
 * 设置 @createdBy 和 @lastUpdateBy 的默认值
 * @date 2018/6/21 下午8:00
 */
@Configuration
@Slf4j
public class AuditingEntityConfiguration implements AuditorAware<String> {
    @Override
    public Optional<String> getCurrentAuditor() {
        RequestDTO requestDTO = ContextHolder.get();
        if (requestDTO == null) {
            log.warn("未登录状态操作数据库");
            return Optional.of("system");
        }
        return Optional.of(requestDTO.getUserInfoDTO().getUsername());
    }
}